/**
 * @description 封装交互逻辑提示，如：alert、confirm、toast等
 * @author Tim
 * note: sdk version >= 1.9.0
 */
import PType from "./PType.class";
import Utils from "./Utils.class";

const noop = () => {}; // 空函数
const defaultModalOptions = {
    // 默认modal配置
    title: "温馨提示",
    showCancel: true,
    confirmText: "确定",
    confirmColor: '#000000',
    cancelText: "取消",
    cancelColor: "#666666",
    success: noop,
    fail: err => {
        console.error("调用show modal发生异常:" + JSON.stringify(err));
    },
    complete: noop
};

export default class Modal {

    /**
     * @description alert弹窗封装
     * @param options {String/Object}参数
     *
     *  {具体参看showModal api接口参数}
     * @return {*} Promise
     */
    static alert(options = {
        content: ""
    }) {
        return new Promise((resolve, reject) => {
            if (PType.isString(options)) options = {
                content: options
            };

            //不显示cancel按钮
            options["showCancel"] = false;
            //成功回调函数
            options["success"] = () => {
                resolve();
            };
            //深度拷贝
            options = Utils.mergeDeep({}, defaultModalOptions, options);

            uni.showModal(options);
        });
    }

    /**
     * @description confirm弹窗封装
     * @param options {String/Object} 参数
     *
     * {具体的参考showModal接口}
     * @return {*} Promise
     */
    static confirm(options = {
        content: "确认执行此操作?"
    }) {
        return new Promise((resolve, reject) => {
            let content = content || "";

            //配置对象
            if (PType.isString(options)) {
                //标题
                options = {
                    content: options
                };
            }

            // 成功的回调函数
            options["success"] = res => {
                if (res.confirm) {
                    resolve();
                    return;
                }
                reject();
            };

            //深度继承
            options = Utils.mergeDeep({}, defaultModalOptions, options);

            uni.showModal(options);
        })
    }

    /**
     * @description toast 弹窗
     * note: icon为空的时候需要对应的sdk版本高于1.9.0以上才会支持
     * @param {*} options {string/object}
     */
    static toast(options = {
        title: ""
    }) {
        if (PType.isString(options)) {
            options = {
                title: options
            };
        }

        if (!options.icon) options["icon"] = "none";
        options["duration"] = 3000; //默认的3s

        uni.showToast(options);
    }

    /**
     * 加载loading
     * @param {*} title 
     * @param {*} mask
     */
    static showLoading(title, mask = true) {
        uni.showLoading({
            title,
            mask
        });
    }

    /**
     * 关闭loading
     */
    static hideLoading() {
        uni.hideLoading();
    }
}